//
//  LBWelcomViewController.swift
//  LBNewWeiBo
//
//  Created by bin on 15/10/30.
//  Copyright © 2015年 bin. All rights reserved.
//     // 1.用户名称
        // 2.背景图片
        // 3.用户头像

import UIKit
import SDWebImage
class LBWelcomViewController: UIViewController {
    
    // 头像的底部约束
    var BottomLayout : NSLayoutConstraint?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 展示子控件
        prepareUI()
        
        if let urlString = LBUserAccout.loadAccount()?.avatar_large {
            // 设置用户的头像
            iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }

    //MARK: - 系统方法：界面显示完成之后调用
    /// 在界面显示完整之后开启动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        // 展示动画
        showAnme()
    }
    
    //MARK: - 展示动画
    private func showAnme() {
    
        BottomLayout?.constant = -UIScreen.mainScreen().bounds.height - (BottomLayout?.constant)!
        UIView.animateWithDuration(1.2, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded() // 重新绘制
            }) { (_) -> Void in
              UIView.animateWithDuration(1.0, animations: { () -> Void in
                self.welcomeTitle.alpha = 1
                }, completion: { (_) -> Void in
                        // 跳转到主页控制器
                    (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
              })
        }
        
        
    }
    
    //MARK: - UI加载准备
    func prepareUI() {
        view.addSubview(bgView) // 背景视图
        view.addSubview(iconView) // 头像
        view.addSubview(welcomeTitle) // 标题
        
        // 设置子控件布局
        // 背景图片
        bgView.translatesAutoresizingMaskIntoConstraints = false // 取消自动布局
        // 添加约束条件 "H: |-0-[subView]-0-|"
        // 水平方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["subView" : bgView]))
        // 垂直方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["subView" : bgView]))
        
        // 头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        // center X
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        // 底部距离
        BottomLayout = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        
        view.addConstraint(BottomLayout!)
        
        // 宽高
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 标题
        welcomeTitle.translatesAutoresizingMaskIntoConstraints = false
        // 顶部与头像相隔16
        view.addConstraint(NSLayoutConstraint(item: welcomeTitle, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 中心X
        view.addConstraint(NSLayoutConstraint(item: welcomeTitle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
    }
    
    
    //MARK: - 懒加载
    
    /// 欢迎标题
    lazy var welcomeTitle : UILabel = {
        let title = UILabel()
        title.text = "欢迎光临"
        title.sizeToFit() // 设置内容自动适应大小
        // 调整 标题透明度
        title.alpha = 0
        return title
    }()
    
    /// 背景图片
    lazy var bgView : UIImageView = {
       let bgView = UIImageView(image: UIImage(named: "ad_background"))
        return bgView
    }()
    
    /// 用户头像
    lazy var iconView : UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 画圆
        iconView.layer.cornerRadius = 42.5 // 半径
        iconView.layer.masksToBounds = true // 裁剪
        
        return iconView
     }()

}
